package weaver.interfaces.suntai.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.interfaces.suntai.utils.CalUtil;
import weaver.interfaces.suntai.utils.InvokeHelper;
import weaver.interfaces.suntai.utils.SunTaiUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-05-12
 * @Description: 采购合同申请流程
 * @ModifyLog:
 **/

public class WeaPayContractAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = Math.abs( requestInfo.getRequestManager().getFormid() );
        User user = requestInfo.getRequestManager().getUser();
        int userid = user.getUID();
        String resultText = "";
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>WeaPayContractAction>>>>>" + requestid );
        try {
            rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
            if (rs.next()) {
                String mainid = Util.null2String( rs.getString( "id" ) );
                writeLog( ">>>>>mainid>>>>" + mainid );
                JSONObject jsonData = new JSONObject();
                //企业编号
                jsonData.put( "companyCode", "01-0070-202202-001" );
                //合同编号
                jsonData.put( "contractCode", Util.null2String( rs.getString( "htbh1" ) ) );
                //合同名称
                jsonData.put( "contractName", Util.null2String( rs.getString( "htmc" ) ) );
                jsonData.put( "contractSecretLev", Util.null2String( rs.getString( "htmj" )));
                String cgf = Util.null2String( rs.getString( "cgf" ) );
                //采购方
                jsonData.put( "purchaserName", SunTaiUtil.getSelectItem( formid, "cgf", cgf ) );
                //采购方统一社会信用代码
                jsonData.put( "purchaserCode", Util.null2String( rs.getString( "cgrdm" ) ) );
                //合同履约状态
                jsonData.put( "contKeepStatus", Util.null2String( rs.getString( "lyztwb" ) ) );
                //采购方统一社会信用代码
                jsonData.put( "purchaseUse", Util.null2String( rs.getString( "cgrdm" ) ) );
                //资金来源
                jsonData.put( "capitalSource", Util.null2String( rs.getString( "zjlywb" ) ) );
                //计价方式
                jsonData.put( "pricingManner", Util.null2String( rs.getString( "jjfswb" ) ) );
                //技术权重
                jsonData.put( "technicalWeight", Util.null2String( rs.getString( "jsqc" ) ) );
                //商务权重
                jsonData.put( "businessWeight", Util.null2String( rs.getString( "swqc" ) ) );
                //付款方式
                jsonData.put( "payWay", Util.null2String( rs.getString( "fkfswb" ) ) );
                //采购方式
                jsonData.put( "purchaseWay", Util.null2String( rs.getString( "cgfswb" ) ) );
                //采购组织形式
                jsonData.put( "purchaseOrgForm", Util.null2String( rs.getString( "cgzzxswb" ) ) );
                //电子招标平台
                jsonData.put( "procurementPlatform", Util.null2String( rs.getString( "dzzbpt" ) ) );
                //合同金额
                jsonData.put( "contractAmount", Util.null2String( rs.getString( "htje" ) ) );
                //币种
                jsonData.put( "currency", Util.null2String( rs.getString( "bzwb" ) ) );
                //汇率
                jsonData.put( "exchangeRate", Util.null2String( rs.getString( "hl" ) ));
                String se = Util.null2String( rs.getString( "se" ) );
                String sevalue =SunTaiUtil.getSelectItem( formid, "se", se );
                sevalue = CalUtil.fDivide2(sevalue.replace( "%",""),"100",4);
                //税率
                jsonData.put( "taxRate", sevalue);
                //签订时间
                jsonData.put( "signTime", Util.null2String( rs.getString( "qdsj" ) ) );
                //生效时间
                jsonData.put( "effectiveDate", Util.null2String( rs.getString( "sxsj" ) ) );
                //开始时间
                jsonData.put( "startTime", Util.null2String( rs.getString( "htkssj" ) ) );
                //结束时间
                jsonData.put( "endTime", Util.null2String( rs.getString( "htjssj" ) ) );
                //履约地点
                jsonData.put( "fulfillmentPlace", Util.null2String( rs.getString( "lyddwb" ) ) );
                //履约评价
                jsonData.put( "supplierEvaluation", Util.null2String( rs.getString( "lypj" ) ) );
                //是否招标
                jsonData.put( "tender", Util.null2String( rs.getString( "sfzbwb" ) ) );
                //采购包和标段编号
                jsonData.put( "blockNumber", Util.null2String( rs.getString( "cgbhbdbh" ) ) );
                //采购包和标段名称
                jsonData.put( "blockName", Util.null2String( rs.getString( "cgbhbdmc" ) ) );
                //采购包或标投预算
                jsonData.put( "investmentBudget", Util.null2String( rs.getString( "cgbbtys" ) ) );
                //备注
                jsonData.put( "note", Util.null2String( rs.getString( "bz1" ) ) );
                //卖方国别
                jsonData.put( "country", Util.null2String( rs.getString( "mfgbwb" ) ) );
                //预算类型
                jsonData.put( "budgetTypes", Util.null2String( rs.getString( "yslxwb" ) ) );
                //评标_评价办法
                jsonData.put( "evaluationMethod", Util.null2String( rs.getString( "pbpjbf" ) ) );
                //发标日期
                jsonData.put( "invitationDate", Util.null2String( rs.getString( "fbrq" ) ) );
                //开标日期
                jsonData.put( "openingDate", Util.null2String( rs.getString( "kbrq" ) ) );
                //履约担保
                jsonData.put( "performanceGuarantee", Util.null2String( rs.getString( "lydbwb" ) ) );
                //履约阶段
                jsonData.put( "performanceStage", Util.null2String( rs.getString( "lyjdwb" ) ) );
                //履约阶段说明
                jsonData.put( "instructions", Util.null2String( rs.getString( "lyjdsm" ) ) );
                //是否变更
                String sfwhtbg = Util.null2String( rs.getString( "sfwhtbg" ) );
                jsonData.put( "whetherToChange", "0".equals( sfwhtbg ) ? "Y" : "N" );
                //卖方名称
                jsonData.put( "supplierName", Util.null2String( rs.getString( "gysmcwb" ) ) );
                //卖方代码
                jsonData.put( "supplierCode", Util.null2String( rs.getString( "gysdm" ) ) );
                //卖方性质
                jsonData.put( "supplierClassify", Util.null2String( rs.getString( "mfxzwb" ) ) );
                //供应商类型
                jsonData.put( "isQualifiedSupplier", Util.null2String( rs.getString( "gyslxwb" ) ) );
                //是否集团内单位
                String sfjtndw = Util.null2String( rs.getString( "sfjtndw" ) );
                jsonData.put( "isInUnitCompany", "0".equals( sfjtndw ) ? "Y" : "N");
                //合同状态
                jsonData.put( "contractStatus", Util.null2String( rs.getString( "htztwb" )));
                //供应商分类
                jsonData.put( "supplierClassification", Util.null2String( rs.getString( "gysflwb" )));
                //具体采购内容简述
                jsonData.put( "purchaseContent", Util.null2String( rs.getString( "jtcgnrjs" )));
                //是否委托招标机构
                jsonData.put( "whetherBidding", Util.null2String( rs.getString( "sfwtzbjgwb" )));
                //采购合同分类
                jsonData.put( "purchaseClass", Util.null2String( rs.getString( "cghtflwb" )));
                //创建人
                jsonData.put( "createBy", Util.null2String( rs.getString( "sqrwb" ) ) );
                //租户编码
                jsonData.put( "tenantCode", "ZDKST" );
                jsonData.put( "status", "rkz");
                //明细1-收款方对象
                JSONArray payBankArray = new JSONArray();
                rs.executeQuery( "select * from " + tablename + "_dt1 where mainid=?", mainid );
                while (rs.next()) {
                    JSONObject payBankJson = new JSONObject();
                    //收款方名称
                    payBankJson.put( "payeeName", Util.null2String( rs.getString( "skfdw" ) ) );
                    //收款方账户
                    payBankJson.put( "payeeAccount", Util.null2String( rs.getString( "skfzh" ) ) );
                    //开户行
                    payBankJson.put( "accountBank", Util.null2String( rs.getString( "khx" ) ) );
                    //收款方账户
                    payBankJson.put( "repayAcctNo", Util.null2String( rs.getString( "skfzh" ) ) );
                    //收款方名称
                    payBankJson.put( "repayName", Util.null2String( rs.getString( "skfdw" ) ) );
                    //开户行
                    payBankJson.put( "branch", Util.null2String( rs.getString( "khx" ) ) );
                    //省市
                    payBankJson.put( "fulfillmentPlace", Util.null2String( rs.getString( "sswb" ) ) );
                    //银联号
                    payBankJson.put( "unionpay", Util.null2String( rs.getString( "ylh" ) ) );
                    payBankJson.put( "payeeRatio", "100" );
                    payBankArray.add( payBankJson );
                }
                jsonData.put( "payBank", payBankArray );

                //明细2-投标人/报价人对象
                JSONArray bidderObjArray = new JSONArray();
                rs.executeQuery( "select * from " + tablename + "_dt2 where mainid=?", mainid );
                while (rs.next()) {
                    JSONObject bidderObjJson = new JSONObject();
                    //投标人/报价人名称
                    bidderObjJson.put( "bidderName", Util.null2String( rs.getString( "tbrbjrmc" ) ) );
                    //投标人/报价人代码
                    bidderObjJson.put( "bidderNumber", Util.null2String( rs.getString( "tbrbjrdm" ) ) );
                    bidderObjArray.add( bidderObjJson );
                }
                jsonData.put( "bidderObj", bidderObjArray );

                //明细3-采购标的
                JSONArray purchObjArray = new JSONArray();
                rs.executeQuery( "select * from " + tablename + "_dt3 where mainid=?", mainid );
                while (rs.next()) {
                    JSONObject purchObjJson = new JSONObject();
                    //采购对象名称
                    purchObjJson.put( "name", Util.null2String( rs.getString( "mc" ) ) );
                    //采购对象型号
                    purchObjJson.put( "model", Util.null2String( rs.getString( "xh" ) ) );
                    //采购对象数量
                    purchObjJson.put( "numberOf", Util.null2String( rs.getString( "sl" ) ) );
                    //采购对象单价
                    purchObjJson.put( "unitPrice", Util.null2String( rs.getString( "dj" ) ) );
                    //计量单位
                    purchObjJson.put( "measuringUnit", Util.null2String( rs.getString( "jldw" ) ) );
                    //生产厂家
                    purchObjJson.put( "manufacturer", Util.null2String( rs.getString( "sccj" ) ) );
                    //用途
                    purchObjJson.put( "use", Util.null2String( rs.getString( "ytwb" ) ) );
                    //生产厂家国别
                    purchObjJson.put( "country", Util.null2String( rs.getString( "sccjgb" ) ) );
                    purchObjArray.add( purchObjJson );
                }
                jsonData.put( "purchObj", purchObjArray );

                //明细5-付款计划
                JSONArray payPlanArray = new JSONArray();
                rs.executeQuery("select * from "+tablename+"_dt5 where mainid=?",mainid);
                while (rs.next()){
                    JSONObject payPlanJson = new JSONObject();
                    //款项名称
                    payPlanJson.put( "progressItems", Util.null2String( rs.getString( "fktjwb" ) ) );
                    //预计时间
                    payPlanJson.put( "progressTimes", Util.null2String( rs.getString( "yjsj" ) ) );
                    //款项金额
                    payPlanJson.put( "paymentAmount", Util.null2String( rs.getString( "fkje" ) ) );
                    //收款方名称
                    payPlanJson.put( "payeeName", Util.null2String( rs.getString( "skfmc" ) ) );
                    payPlanArray.add(payPlanJson);
                }
                jsonData.put( "payPlans", payPlanArray );
                writeLog( ">>>>>jsonData>>>>>" + jsonData.toJSONString() );
                resultText = InvokeHelper.addPaycont( jsonData.toJSONString() );
                writeLog( ">>>>>resultText>>>>>" + resultText );
                JSONObject resultJson = JSONObject.parseObject( resultText );
                String code = Util.null2String( resultJson.getString( "code" ) );
                String message = Util.null2String( resultJson.getString( "message" ) );
                if (!"200".equals( code )) {
                    requestInfo.getRequestManager().setMessageid( "111111" );
                    requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + message );
                    return Action.FAILURE_AND_CONTINUE;
                }
                SunTaiUtil.saveCallInterLog( "采购合同新增接口", message, requestid, userid );
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
